package com.bba.fms.common.ezspecial.service;

import com.bba.fms.common.ezspecial.core.EzSpecial;
import com.bba.fms.common.ezspecial.dto.FieldDto;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;

/**
 * @BelongsProject: fms
 * @Author: 王志鹏
 * @CreateTime: 2019-10-14 14:18
 * @description: TODO
 */
@Slf4j
public class ServiceUtil {
    private ServiceUtil(){}

    public static FieldDto getFiledByEzSpecialValue(Class cls, String v) {
        FieldDto fileDto = null;

        Field[] fields = cls.getDeclaredFields();
        for (Field f : fields) {
            f.setAccessible(true);
            if (f.isAnnotationPresent(EzSpecial.class)) {

                EzSpecial ezSpecial = f.getAnnotation(EzSpecial.class);
                if ((ezSpecial.value()).equals(v)) {
                    fileDto = new FieldDto();
                    String[] check = ezSpecial.check();
                    fileDto.setCheck(check);
                    fileDto.setColoumName(f.getName());
                    fileDto.setRegular(ezSpecial.reglar().getRule());
                    fileDto.setRequire(ezSpecial.require());
                }
            }
        }

        if (fileDto == null){
            log.info(String.format("未找到该类属性 %s !",v));
            return null;
        }
        return fileDto;
    }

}
